package algorithms.que14;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

import java.util.Random;

/**
 * @功能
 * @创建时间 2020/9/8
 */
public class E1d4d19 {
    public static  void min(int[][] a)
    {
        int Rlo=1;
        int Rhi=a.length;
        int Rmid;
        //
        int Clo=1;
        int Chi=a[0].length;
        int Cmid;

        while(Rlo<Rhi && Clo<Chi)
        {
            Rmid=(Rlo+Rhi)/2;
            Cmid=(Clo+Chi)/2;
            if(a[Rmid-1][Cmid]<a[Rmid][Cmid])
                Rhi=Rmid-1;
            else if(a[Rmid][Cmid]>a[Rmid+1][Cmid])
                Rlo=Rmid+1;
            else if(a[Rmid][Cmid-1]<a[Rmid][Cmid])
                Chi=Cmid-1;
            else if(a[Rmid][Cmid]>a[Rmid][Cmid+1])
                Clo=Cmid+1;
            else
            {
                StdOut.printf("row=%d,col=%d",Rmid,Cmid);
                return ;
            }
        }
        StdOut.print("can't find");
    }

    public static void main(String[] args)
    {
        int N=Integer.parseInt(args[0]);
        int[][] a=new int[N][N];
        int k=0;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                a[i][j]= StdRandom.uniform(N*N);//数组值会有重复。
                StdOut.printf("%6d ",a[i][j]);
            }//end for
            StdOut.println();
        }//end for
        min(a);
        //StdOut.println(min(a));
    }//end main
}
